﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Windows.Forms;
using System.Threading;
using System.IO;
using CrawlerCore;
using WCrawlerLib;

namespace WCrawler.TestPages
{
    public partial class frmReCaptcha : System.Web.UI.Page
    {
        private int postId = 18; //Bad Teacher
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["humanverify"] != null)
                    txtHumanVerify.Text = Session["humanverify"].ToString();
                if (Session["recaptcha"] != null)
                    imgReCaptcha.ImageUrl = Session["recaptcha"].ToString();
            }
            else
            {
                Session["humanverify"] = null;
                Session["recaptcha"] = null;
            }
        }

        public void ucForumControlSelectedIndexChanged(object sender, EventArgs e)
        {
            //Session["ucForum_GridViewForums"] = (GridView)ucForumControl.FindControl("gridViewForums");
            //Session["ucForum_SelectedValue"] = ucForumControl.SelectedValue;

            Forum objForum = new Forum();
            objForum = objForum.GetForum(ucForumControl.SelectedValue);
            txtUrl.Text = objForum.ForumUrl + "/" + objForum.DefaultUrl;
            txtUsername.Text = objForum.Account.UserName;
            txtPassword.Text = objForum.Account.Password;

            //fid
            int fValue = -1;
            try
            {
                ForumTopic objForumTopic = new ForumTopic();
                objForumTopic = objForumTopic.GetExactlyPostTopicValue(objForum.ForumId, postId); //Bad Teacher
                fValue = objForumTopic.fValue.Value;
            }
            catch
            {
                fValue = -1;
            }

            //post content
            try
            {
                PostDetail objPostDetail = new PostDetail();
                objPostDetail = objPostDetail.GetExactlyPostDetail(postId, objForum.LanguageId);
                txtTitle.Text = objPostDetail.PostTitle;
                txtMessage.Text = objPostDetail.PostContent;
            }
            catch
            {

            }

            ForumTemplateDetail objForumTemplateDetail = new ForumTemplateDetail();
            txtNewThreadUrl.Text = objForumTemplateDetail.GetNewThreadUrl(objForum.ForumId, objForum.ForumTypeId);
            txtNewThreadUrl.Text = txtNewThreadUrl.Text.Replace("&s=[sessionhash]", "");
            txtNewThreadUrl.Text = GeneralClass.GetFullUrl(objForum.ForumUrl, txtNewThreadUrl.Text.Replace("[fid]", fValue.ToString()));
        }

        protected void btnLoad_Click(object sender, EventArgs e)
        {
            string url = txtUrl.Text;
            string newthreadUrl = txtNewThreadUrl.Text;
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            
            AutoResetEvent resultEvent = new AutoResetEvent(false);
            string result = null;
            bool visible = true;
            IEForumBrowser browser = new IEForumBrowser(visible, txtUrl.Text, newthreadUrl, username, password, resultEvent);
            // wait for the third thread getting result and setting result event
            EventWaitHandle.WaitAll(new AutoResetEvent[] { resultEvent });
            // the result is ready later than the result event setting somtimes 
            while (browser == null || browser.HtmlResult == null) Thread.Sleep(5);
            
            Session["browser"] = browser;
            result = browser.HtmlResult;
            if (!visible) browser.Dispose();

            string path = Request.PhysicalApplicationPath;
            TextWriter tw = new StreamWriter(path + "result.html");
            tw.Write(result);
            tw.Close();

            Response.Output.WriteLine("<script>window.open ('result.html','mywindow','location=1,status=0,scrollbars=1,resizable=1,width=600,height=600');</script>");
        }

        protected void btnGetReCaptcha_Click(object sender, EventArgs e)
        {
            try
            {
                AutoResetEvent resultEvent = new AutoResetEvent(false);
                bool visible = false;
                IEForumBrowser browser = new IEForumBrowser(txtUrl.Text, txtUsername.Text, txtPassword.Text, txtNewThreadUrl.Text, resultEvent, visible);
                // wait for the third thread getting result and setting result event
                EventWaitHandle.WaitAll(new AutoResetEvent[] { resultEvent });
                // the result is ready later than the result event setting somtimes 
                while (browser == null) Thread.Sleep(5);
                //txtHumanVerify.Text = browser.humanverify;
                //imgReCaptcha.ImageUrl = browser.recaptcha_image;
                Session["humanverify"] = browser.humanverify;
                Session["recaptcha"] = browser.recaptcha_image;
                Response.Redirect(Request.RawUrl);
                
            }
            catch
            {

            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            AutoResetEvent resultEvent = new AutoResetEvent(false);
            bool visible = true;
            IEForumBrowser browser = new IEForumBrowser(txtUrl.Text, txtUsername.Text, txtPassword.Text, txtNewThreadUrl.Text, txtTitle.Text,txtMessage.Text, resultEvent, visible);
            // wait for the third thread getting result and setting result event
            EventWaitHandle.WaitAll(new AutoResetEvent[] { resultEvent });
            // the result is ready later than the result event setting somtimes 
            while (browser == null) Thread.Sleep(5);
        }
    }
}